<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<title></title>
	</head>

	<body>
		<div>
			解析前字符串 <br>
			<textarea id="otext" cols="100%" rows="6"></textarea>
			<br><button onclick="parseString()"> 解析</button>
			<br> 编译前模版 <br>
			<textarea id="otemplate" cols="100%" rows="3"></textarea>
			<br> 数列 <br>
			<textarea id="olist" cols="100%" rows="6"></textarea> <br>
			<div>
				<button onclick="getListFromTemplate()"> 编译</button> </div>
			编译后 <br>
			<textarea id="oresult" cols="100%" rows="20"></textarea>
		</div>
	</body>
	<script>
		function parseString() {
			var t1 = document.getElementById("otext").value;
			t1 = t1.toLowerCase().replace(/long/g, "").replace(/int/g, "").replace(/bigdecimal/g, "")
				.replace(/string/g, "").replace(/\s+/g, "").replace(/[\r\n]/g, "");
			var att = t1.split(",");
			var l = "";
			for(var i in att) {
				l += att[i] + ','
			}
			document.getElementById("olist").value = l;
		}

		function getJSON(str) {
			if(typeof str == 'string') {
				try {
					var obj = JSON.parse(str);
					if(typeof obj == 'object' && obj) {
						return obj;
					} else {
						return null;
					}

				} catch(e) {
					// console.log('error：'+str+'!!!'+e);
					return null;
				}
			}
			//  console.log('It is not a string!')
		}

		function getListFromTemplate() {
			var t1 = document.getElementById("otemplate").value.replace(/\s+/g, "");
			var l1 = document.getElementById("olist").value.replace(/\s+/g, "");
			var list1 = getJSON(l1);
			if(!list1) {
				list1 = l1.split(",")
			}
			var t;
			var s = "";
			if(Array.isArray(list1)) {
				for(var i = 0; i < list1.length; i++) {
					var obj = list1[i];
					t = t1.replace(/#i#/g, i);
					if(typeof obj === 'object') {
						for(var key in obj) {
							t = t.replace("((" + key + "))", obj[key]);
						}
					} else {
						if(!obj) break;
						t = t.replace("(())", obj);
					}

					s += t + '\r\n';
				}
			}

			document.getElementById("oresult").value = s;
			//console.log(list1);

		}
	</script>

</html>